//
//  PTSManageTool.m
//  PTS80
//
//  Created by 黄杰 on 15/10/31.
//  Copyright © 2015年 黄杰. All rights reserved.
//

#import "PTSManageTool.h"

@implementation PTSManageTool

#pragma mark - 获取管理对象
+ (AFHTTPSessionManager *)setupHttpManageWithURL:(NSString *)url
{
    // 创建请求管理对象
    AFHTTPSessionManager *mgr = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:url]];
    
    // 说明服务器返回json数据
    mgr.requestSerializer.timeoutInterval = 10;
    mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", @"text/javascript", @"text/json", @"text/plain", nil];
    
    return mgr;
}

+ (void)PostWithURL:(NSString *)URL parameters:(NSDictionary *)parameters success:(void (^)(id))success failure:(void (^)(PTSError *))failure
{
    // 获取管理对象
    AFHTTPSessionManager *mgr = [self setupHttpManageWithURL:URL];
    
    [mgr POST:URL parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
       
    
        // 如果有数据
        if ([responseObject count]) {

            success(responseObject);
//            if ([responseObject[@"code"] integerValue] == 200) { // 成功
//                // 成功则返回json
//                success(responseObject);
//                // 成功则返回成功信息
//                PTSError *psError = [[PTSError alloc] init];
//                psError.errorCode = responseObject[@"code"];
//                psError.errorString = @"成功";
//            } else if ([responseObject[@"returns"] integerValue]) {
//                // 成功则返回json
//                success(responseObject);
//            } else {
//                // 失败则返回失败信息
//                PTSError *psError = [[PTSError alloc] init];
//                psError.errorCode = responseObject[@"code"];
//                psError.errorString = responseObject[@"message"];
//                failure(psError);
//            }
            
        } else {
            // 没有数据则返回空代码
            PTSError *psError = [[PTSError alloc] init];
            psError.errorCode = @"404";
            psError.errorString = @"数据为空!";
            failure(psError);
            
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 失败则返回失败信息
        PTSError *psError = [[PTSError alloc] init];
        psError.errorCode = error.localizedDescription;
        psError.errorString = @"网络请求超时";
        failure(psError);
        
    }];

}

+ (void)GetWithURL:(NSString *)URL parameters:(NSDictionary *)parameters success:(void (^)(id))success failure:(void (^)(PTSError *))failure
{
    
    // 获取管理对象
    AFHTTPSessionManager *mgr = [self setupHttpManageWithURL:URL];
    
    [mgr GET:URL parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        // 如果有数据
        if ([responseObject count]) {
            success(responseObject);
//            if ([responseObject[@"code"] integerValue] == 200) { // 成功
//                // 成功则返回json
//                success(responseObject);
//                // 成功则返回成功信息
//                PTSError *psError = [[PTSError alloc] init];
//                psError.errorCode = responseObject[@"code"];
//                psError.errorString = @"成功";
//                failure(psError);
//            } else if ([responseObject[@"returns"] integerValue]) {
//                // 成功则返回json
//                success(responseObject);
//            } else {
//                // 失败则返回失败信息
//                PTSError *psError = [[PTSError alloc] init];
//                psError.errorCode = @"404";
//                psError.errorString = responseObject[@"message"];
//                failure(psError);
//            }
            
        } else {
            // 没有数据则返回空代码
            PTSError *psError = [[PTSError alloc] init];
            psError.errorCode = responseObject[@"code"];
            psError.errorString = @"数据为空!";
            failure(psError);
            
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        // 失败则返回失败信息
        PTSError *psError = [[PTSError alloc] init];
        psError.errorCode = error.localizedDescription;
        psError.errorString = @"网络请求超时";
        failure(psError);
        
    }];
}

+ (void)PostWithURL:(NSString *)URL parameters:(NSDictionary *)parameters formDataArray:(NSArray *)formDataArray success:(void (^)(id))success failure:(void (^)(PTSError *))failure
{
    
    // 获取管理对象
    AFHTTPSessionManager *mgr = [self setupHttpManageWithURL:URL];
    
    [mgr POST:URL parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull totalformData) {
        
        for (MyFormData *formData in formDataArray) {
            [totalformData appendPartWithFileData:formData.data name:formData.name fileName:formData.fileName mimeType:formData.mimeType];
        }
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        // 如果有数据
        if ([responseObject count]) {
            
            success(responseObject);
//            if ([responseObject[@"code"] integerValue] == 200) { // 成功
//                // 成功则返回json
//                success(responseObject);
//                // 成功则返回成功信息
//                PTSError *psError = [[PTSError alloc] init];
//                psError.errorCode = responseObject[@"code"];
//                psError.errorString = @"成功";
//                failure(psError);
//            } else if ([responseObject[@"returns"] integerValue]) {
//                // 成功则返回json
//                success(responseObject);
//            } else {
//                // 失败则返回失败信息
//                PTSError *psError = [[PTSError alloc] init];
//                psError.errorCode = responseObject[@"code"];
//                psError.errorString = responseObject[@"message"];
//                failure(psError);
//            }
            
        } else {
            // 没有数据则返回空代码
            PTSError *psError = [[PTSError alloc] init];
            psError.errorCode = @"404";
            psError.errorString = @"数据为空!";
            failure(psError);
            
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        // 失败则返回失败信息
        PTSError *psError = [[PTSError alloc] init];
        psError.errorCode = error.localizedDescription;
        psError.errorString = @"网络请求超时";
        failure(psError);
        
    }];
    
}

@end
